package com.example.mykotilin.paging2.repository.inDb

import androidx.paging.ExperimentalPagingApi
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.example.mykotilin.paging2.RedditApi
import com.example.mykotilin.paging2.db.RedditDb
import com.example.mykotilin.paging2.repository.RedditPostRepository
import com.example.mykotilin.paging2.vo.RedditPost
import kotlinx.coroutines.flow.Flow

class DbRedditPostRepository(val db: RedditDb, private val redditApi: RedditApi) : RedditPostRepository {

    @OptIn(ExperimentalPagingApi::class)
    override fun postOfSubreddit(subReddit: String, pageSize: Int) = Pager(
        config = PagingConfig(pageSize),
        remoteMediator = PageKeyedRemoteMediator(db, redditApi, subReddit)
    ) {
        db.posts().postsBySubreddit(subReddit)
    }.flow
}